package com.carnation._executor;

import java.util.concurrent.*;

public class ThreadPoolTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executorService = getExecutor();
        //1： JDK从1.6开始，允许空闲核心线程超时释放，节省资源。
        // executorService.allowCoreThreadTimeOut(true);

        //2：execute提交Runnable
        executorService.execute(() -> {
            System.out.println(1);
        });

        //3：submit提交Runnable,Runnable无返回值
        executorService.submit(() -> {
            System.out.println(2);
        });

        //4：submit提交Callable,Callable有返回值。
        Future<Integer> future4 = executorService.submit(() -> {
            return 4;
        });
        System.out.println(future4.get());

        Future<?> future5 = executorService.submit(new FutureTask<>(() -> {
            return 5;
        }));
        // Null？
        System.out.println(future5.get());

        executorService.shutdown();
    }

    private static ThreadPoolExecutor getExecutor() {
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 3, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(10));
        return executorService;
    }

}
